package com.atguigu.yygh.cmn.controller;

import com.atguigu.yygh.cmn.service.DictService;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.model.cmn.Dict;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/admin/cmn/dict")
@Api(tags="数据字典")
public class DistController {
 @Autowired
  private DictService dictService;


  //根据数据id查询子数据列表

    @GetMapping("{id}")
    @ApiOperation(value = "根据数据id查询子数据列表")
    public Result   selectById(@PathVariable Long id ){


        List<Dict> list  = dictService.selectById(id);


        return Result.ok(list);
    }



  // 导出数据字典的接口
    @ApiOperation(value = "数据字典下载到excel表格",notes = "export", produces = "application/octet-stream")
  @GetMapping("expertData")
    public void  expertDict(HttpServletResponse  response){

        dictService.expertDiceData(response);

  }

 //导入数据字典
    @PostMapping("importDict")
    @ApiOperation(value = "导入数据字典")
    public  Result importDict(MultipartFile file){

   dictService.importDictData(file);

   return Result.ok();

    }


   //查询所有的数据字典
    @GetMapping("findAll")
    @ApiOperation(value = "查询所有的数据字典")
    public  Result  fingAll(){


        List<Dict>  list =dictService.findAll();
        return Result.ok(list);
    }


    //根据dictcode和value查询
    @GetMapping("getName/{dictCode}/{value}")
    public  String getName(@PathVariable String  dictCode,
                           @PathVariable String value){

         String dictName   =  dictService.getDictName(dictCode,value);


         return dictName;
    }
 //更具value查询
   @GetMapping("getName/{value}")
    public  String  getName(@PathVariable String value){

            String dictName   = dictService.getDictName("",value);

            return dictName;
   }


 //根据dictCode获取下级节点
    @ApiOperation(value = "根据dictCode获取下级节点")
    @GetMapping("findByDictCode/{dictCode}")
    public  Result  findByDictCode(@PathVariable  String dictCode){


          List<Dict> list    = dictService.findByDictCode(dictCode);

          return  Result.ok(list);

    }


}
